package cn.edu.scau.cmi.wuyihan.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import cn.edu.scau.cmi.wuyihan.composite.consistent.Organization;
import cn.edu.scau.cmi.wuyihan.composite.consistent.Person;
import cn.edu.scau.cmi.wuyihan.composite.consistent.Team;

public class ConsistentCompositeClient {

	public static void main(String[] args) {
		action();
	}
	
	public static void action() {
		System.out.print("请输入您要创建的组织名称：");
		Scanner scanner = new Scanner(System.in);
		String organization = scanner.nextLine();
		Team replaceTeam = null;
		List<Organization> teams = new ArrayList<>();
		List<Organization> teams1 = new ArrayList<>();
		
		if (organization != null) {
			while (true) {
				System.out.print("您是否要继续创建团队或者个人（输入Y代表是，输入N代表否） ：");
				String string = scanner.nextLine();
				if ("Y".equalsIgnoreCase(string)) {
					System.out.print("请输入您是要创建团队还是个人（输入T代表团队，P代表个人）：");
					String create = scanner.nextLine();
					if ("T".equalsIgnoreCase(create)) {
						System.out.print("请输入您要创建的团队名称：");
						String teamName = scanner.nextLine();
						System.out.print("请输入该团队的父团队（输入N表示没有父团队，如果有，格式为xxx-xxx）：");
						String parentName = scanner.nextLine();
						if ("N".equalsIgnoreCase(parentName)) {
							Team child = new Team(teamName);
							teams.add(child);
							teams1.add(child);
						}else {
							
							String parents[] = parentName.split("-");
							for(int i=1;i<parents.length;i++) {
								for(Organization organization2 : teams1) {
									if (parents[i].equalsIgnoreCase(organization2.getName())) {
										replaceTeam = (Team)organization2;
										teams1 = replaceTeam.getChildren();
										
										break;
									}
								}
							}
							teams1 = teams;
							replaceTeam.addChildren(new Team(teamName));
							replaceTeam = null;
							

						}
						
						while(true) {
							System.out.print("请问您是否还要继续创建团队（输入Y代表是，输入N代表否）：");
							create = scanner.nextLine();
							if ("N".equalsIgnoreCase(create)) {
								break;
							}
							System.out.print("请输入您要创建的团队名称：");
							teamName = scanner.nextLine();
							
							System.out.print("请输入该团队的父团队（输入N表示没有父团队，如果有，格式为xxx-xxx）：");
							parentName = scanner.nextLine();
							if ("N".equalsIgnoreCase(parentName)) {
								Team child = new Team(teamName);
								teams.add(child);
								teams1.add(child);
							}else {			
								

								String parents[] = parentName.split("-");
								for(int i=1;i<parents.length;i++) {
									for(Organization organization2 : teams1) {
										if (parents[i].equalsIgnoreCase(organization2.getName())) {
											replaceTeam = (Team)organization2;
											teams1 = replaceTeam.getChildren();
											
											break;
										}
									}
								}
								teams1 = teams;
								replaceTeam.addChildren(new Team(teamName));
								replaceTeam = null;
								
								
							}
							
							
						}
						
					}else if ("P".equalsIgnoreCase(create)) {
						System.out.print("请输入您要创建的个人名称：");
						String personName = scanner.nextLine();
						System.out.print("请输入该人所属的团队名称：（输入N表示没有父团队，如果有，格式为xxx-xxx）");
						String belongName = scanner.nextLine();
						if (belongName.equalsIgnoreCase("N")) {
							teams.add(new Person(personName));
						}

						else {

							String parents[] = belongName.split("-");
							for(int i=1;i<parents.length;i++) {
								for(Organization organization2 : teams1) {
									if (parents[i].equalsIgnoreCase(organization2.getName())) {
										replaceTeam = (Team)organization2;
										teams1 = replaceTeam.getChildren();
										
										break;
									}
								}
							}
							teams1 = teams;
							replaceTeam.addChildren(new Person(personName));
							replaceTeam = null;
							
						}
						
						
						while(true) {
							System.out.print("请问您是否还要继续创建个人：（输入Y or N）");
							create = scanner.nextLine();
							if (create.equalsIgnoreCase("N")) {
								break;
							}
							System.out.print("请输入您要创建的个人名称：");
							personName = scanner.nextLine();
							System.out.print("请输入该人所属的团队名称：（输入N表示该人不属于任何团队，如果有，格式为xxx-xxx）");
							belongName = scanner.nextLine();
							if (belongName.equalsIgnoreCase("N")) {
								teams.add(new Person(personName));
							}else {
								
								String parents[] = belongName.split("-");
								for(int i=1;i<parents.length;i++) {
									for(Organization organization2 : teams1) {
										if (parents[i].equalsIgnoreCase(organization2.getName())) {
											replaceTeam = (Team)organization2;
											teams1 = replaceTeam.getChildren();
											
											break;
										}
									}
								}
								teams1 = teams;
								replaceTeam.addChildren(new Person(personName));
								replaceTeam = null;
								
							}
							
	
						}
					}
				}else if("N".equalsIgnoreCase(string)){
					//输出结构
					System.out.println();
					System.out.println();
					System.out.println();
					System.out.println("您创建的组织结构如下：");
					System.out.print("(组织，"+organization + "）"+"  --->  ");
					StringBuilder stringBuilder = new StringBuilder();
					for(Organization organization2 : teams) {
						if (organization2 instanceof Person) {
							stringBuilder.append("（个人，"+organization2.getName()+"）  ");
						}else if (organization2 instanceof Team) {
							stringBuilder.append("（团队，" + organization2.getName() + "）  ");
						}
					}
					System.out.println(stringBuilder.toString());
				
					for(Organization team : teams1) {
						if (team instanceof Team) {
							System.out.println(((Team)team).toString());
						}
					}
					break;
				}
			}
		}
	}

}
